package com.gua.jbs.serviceImpl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.gua.jbs.dao.IStockDao;
import com.gua.jbs.daoImpl.StockDao;
import com.gua.jbs.pojo.Jbspst;
import com.gua.jbs.service.IStockService;
import com.gua.jbs.utility.ApplicationException;
import com.gua.jbs.utility.DBConstant;
import com.gua.jbs.utility.ExceptionMessage;
import com.gua.jbs.utility.StringUtil;

/**
 * Service for maintaining the stock
 * 
 * @author Piyush
 * 
 */
public class StockService extends BaseService implements IStockService {
	private static final Logger logger = Logger.getLogger(StockService.class);
	private IStockDao stockDao = null;

	public StockService() {
		stockDao = new StockDao();
	}

	public boolean addStockForProduct(String productId, String prevStock,
			String newStock) throws ApplicationException {
		boolean stockadded = false;
		try {
			Jbspst jbspst = new Jbspst();
			jbspst.setProdId(productId);
			jbspst.setPrevStock(prevStock);
			jbspst.setCurrentStock(newStock);
			jbspst.setIsAdded(DBConstant.IS_ADDED_YES);
			jbspst.setIsActive(DBConstant.IS_ACTIVE_YES);
			jbspst.setCreateDate(new Date());
			jbspst.setModifyDate(new Date());
			jbspst.setCreatedBy("Piyush");
			jbspst.setCreatedBy("Piyush");
			stockadded = stockDao.addStock(jbspst);
		} catch (ApplicationException ae) {
			throw ae;
		} catch (Exception e) {
			logger.error("StockService : addStockForProduct() method --->"
					+ e.getMessage());
			throw new ApplicationException(
					ExceptionMessage.PRODUCT_STOCK_ADD_UNABLE);
		}
		return stockadded;
	}

	/**
	 * Method to get the product stock
	 * 
	 * @param productId
	 */
	public int getProductStock(String productId) throws ApplicationException {
		int stock = 0;
		if (StringUtil.isNullOrEmpty(productId)) {
			try {
				stock = stockDao.getStockByProductId(productId);
			} catch (ApplicationException ae) {
				throw ae;
			}
		} else
			throw new ApplicationException(ExceptionMessage.INVALID_PRODUCT_ID);
		return stock;
	}

	/**
	 * Method to get the products stock
	 * 
	 * @param productId
	 */
	public List<Object[]> getAllProductStocks()
			throws ApplicationException {
		List<Object[]> productStockList = null;
		try {
			productStockList = stockDao.getAllProductStock();
		} catch (ApplicationException ae) {
			throw ae;
		}
		return productStockList;
	}
}
